package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.redisson.api.RList;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author raccoon@littleraccoon.xyz
 * @since 2022/12/03 13:36
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    // mapper
    @Autowired
    PanelMapper panelMapper; // tb_panel
    @Autowired
    PanelContentMapper panelContentMapper; // tb_panel_content
    @Autowired
    ItemCatMapper itemCatMapper; // tb_item_cat
    // converter
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    ProductCateConverter productCateConverter;
    // redis
    @Autowired
    RedissonClient redissonClient;

    @Override
    public Set<PanelDto> homepage() {
        RSet<PanelDto> panelCache = redissonClient.getSet(GlobalConstants.HOMEPAGE_CACHE_KEY);
        if (panelCache.isEmpty()) {
            Example example = new Example(Panel.class);
            example.createCriteria().andEqualTo("position", GlobalConstants.HEADER_PANEL_ID);
            panelMapper.selectByExample(example).forEach(x -> {
                panelMapper.selectPanelContentById(x.getId()).forEach(y -> {
                    y.setPanelContentItems(panelContentMapper.selectPanelContentAndProductWithPanelId(y.getId()));
                    panelCache.add(contentConverter.panel2Dto(y));
                });
            });
            panelCache.expire(GlobalConstants.HOMEPAGE_EXPIRE_TIME, TimeUnit.SECONDS);
        }
        return panelCache;
    }

    @Override
    public List<PanelContentDto> navigation() {
        RList<PanelContentDto> navigationList = redissonClient.getList(GlobalConstants.HEADER_PANEL_CACHE_KEY);
        if (navigationList.isEmpty()) {
            Example example = new Example(PanelContent.class);
            example.createCriteria().andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);
            panelContentMapper.selectByExample(example).forEach(x -> navigationList.add(contentConverter.panelContent2Dto(x)));
            navigationList.expire(GlobalConstants.HOMEPAGE_EXPIRE_TIME, TimeUnit.SECONDS);
        }
        return navigationList;
    }

    @Override
    public List<ProductCateDto> categories() {
        RList<ProductCateDto> categories = redissonClient.getList(GlobalConstants.PRODUCT_CATE_CACHE_KEY);
        if (categories.isEmpty()) {
            itemCatMapper.selectAll().forEach(x -> categories.add(productCateConverter.item2Dto(x)));
            categories.expire(GlobalConstants.PRODUCT_CATE_EXPIRE_TIME, TimeUnit.DAYS);
        }
        return categories;
    }
}
